const express = require('express')
const app = express()
const port = 3000
var mysql = require('mysql')
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

var connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: 'root',
    database: 'dgtle'
})

connection.connect()

app.all('*', function(req, res, next) {   // 设置允许跨域的域名，*代表允许任意域名跨域
      
    res.header('Access-Control-Allow-Origin', '*');   // 允许的header类型
      
    res.header('Access-Control-Allow-Headers', 'content-type');   // 跨域允许的请求方式
      
    res.header('Access-Control-Allow-Methods', 'DELETE,PUT,POST,GET,OPTIONS');  
    if (req.method.toLowerCase() === 'options') {     res.send(200);  // 让options尝试请求快速结束
           } else {     next();   }
});
// 首页数据
app.get('/index', (req, res) => {
        connection.query('SELECT * from dgtle_index limit 0,10', function(err, rows, fields) {
            res.send({
                status: 200,
                data: rows
            })
        })
    })
    // 点评数据
app.get('/comment', (req, res) => {
        connection.query('SELECT * from dgtle_comment', function(err, rows, fields) {
            res.send({
                status: 200,
                data: rows
            })
        })
    })
    // 点评手机
app.get('/newphone', (req, res) => {
    connection.query('SELECT * from dgtle_newphone', function(err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})
app.get('/newphone', (req, res) => {
    connection.query('SELECT * from dgtle_newphone', function(err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})
app.get('/hotphone', (req, res) => {
        connection.query('SELECT * from dgtle_hotphone', function(err, rows, fields) {
            res.send({
                status: 200,
                data: rows
            })
        })
    })
    // 文章数据
app.put('/article', (req, res) => {
        const num = req.body.num
        const num1 = (num - 1) * 17
        const num2 = num * 17
        if (num == 0) {
            connection.query(`select * from dgtle_article limit 0,17`, function(err, rows, fields) {
                res.send({
                    status: 200,
                    data: rows
                })
            })
        } else {
            connection.query(`select * from dgtle_article limit ${num1},${num2}`, function(err, rows, fields) {
                res.send({
                    status: 200,
                    data: rows
                })
            })
        }

    })
    // 众测数据
app.get('/evaluating', (req, res) => {
    connection.query(`select * from dgtle_testin_product limit 0,8 `, function(err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})

app.get('/experience', (req, res) => {
    connection.query(`select * from dgtle_testin_featured limit 0,10`, function(err, rows, fields) {
        res.send({
            status: 200,
            data: rows
        })
    })
})
app.put('/activity', (req, res) => {
        const aclass = req.body.aclass
        console.log(aclass);
        connection.query(`select * from dgtle_activity where activity_time like '%${aclass}%'  `, function(err, rows, fields) {
            res.send({
                status: 200,
                data: rows
            })
        })
    })
    // 闲置数据
app.put('/aside', (req, res) => {
        const num = req.body.num + 1
        if (num == 0) {
            connection.query(`select * from dgtle_sale limit 0,20 `, function(err, rows, fields) {
                res.send({
                    status: 200,
                    data: rows
                })
            })
        } else {
            const n = (num - 1) * 20
            const m = num * 20
            connection.query(`select * from dgtle_sale limit ${n},${m} `, function(err, rows, fields) {
                res.send({
                    status: 200,
                    data: rows
                })
            })
        }

    })
    // 鲸闻数据
app.put('/news', (req, res) => {
        const num = req.body.flag
        if (num == 1) {
            connection.query(`select * from dgtle_live limit 0,15`, function(err, rows, fields) {
                res.send({
                    status: 200,
                    data: rows
                })
            })
        } else if (num == 2 || num == 0) {
            connection.query(`select * from dgtle_news_message `, function(err, rows, fields) {
                res.send({
                    status: 200,
                    data: rows
                })
            })
        } else if (num == 3) {
            connection.query(`select * from dgtle_dayword limit 0,15`, function(err, rows, fields) {
                res.send({
                    status: 200,
                    data: rows
                })
            })
        }

    })
    // connection.end() 断开连接

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})